<template>
<!-- 角色权限 -->
  <el-dialog :visible.sync="isShow" v-if="isShow" title="角色权限" width="50%" :before-close="handleClose" top="100px" :close-on-click-modal="false">
    <div class="border-box">
      <el-form ref="ruleForm" :model="formParams" :rules="ruleForm" label-width="">
        <div class="flex item-box" v-for="(item,index) in itemData" :key="index">
          <div class="title width25">
            {{ item.label }}<span :class="item.pass?'mustPass':'noMustPass'">*</span>
          </div>
          <div class="content width80">
            <el-form-item class="item-clear" v-show="item.key=='role_name'" label="" prop="role_name">
              <el-input class="width40" type="text" v-model="formParams.role_name" placeholder="请输入角色名称"/>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='remark'" label="" prop="remark">
              <el-input type="text" v-model="formParams.remark" placeholder="请输入角色描述"/>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="handleClose">取 消</el-button>
      <el-button size="medium" type="primary" @click="handleConfirm('ruleForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  roleAdd,
  roleEdit
} from '@/api/system.js'
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: function () {
        return {}
      }
    },
  },
  data () {
    return {
      isShow: false,
      itemData:[
        { label: '角色名称', key: 'role_name' ,pass:true},
        { label: '角色描述', key: 'remark' },
      ],
      formParams: {
        role_name: '',
        remark: '',
      },
      ruleForm: {
        role_name: [{ required: true, trigger: 'blur', message: '请输入角色名称', }],
      }
    }
  },
  watch: {
    show: function () {
      let that = this;
      that.isShow = that.show
      if (that.data) {
        that.formParams = JSON.parse(JSON.stringify(that.data))
      } else {
        that.formParams = {
          role_name: '',
          remark: '',
        }
      }
    }
  },
  mounted () { },
  methods: {
    // 确认
    handleConfirm (formName) {
      let that = this, params = JSON.parse(JSON.stringify(that.formParams));
      that.$refs[formName].validate((valid) => {
        if (valid) {
          // 新增
          if (!params.id) {
            roleAdd(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          } else {
            roleEdit(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          }
        } else {
          return false;
        }
      });
    },
    // 取消
    handleClose () {
      this.$emit('onHideDetails', false)
    },
  }
}
</script>

<style scoped lang="scss">
</style>
